import axios from 'axios';

// 替换为你的API Key和Secret Key
const API_KEY = 'your_api_key';
const SECRET_KEY = 'your_secret_key';

// 获取AccessToken的函数
const getAccessToken = async () => {
  try {
    const response = await axios.post(
      'https://open.volcengine.com/api/v1/doubao/openapi/auth/get_access_token',
      {
        api_key: API_KEY,
        secret_key: SECRET_KEY
      }
    );
    return response.data.data.access_token;
  } catch (error) {
    console.error('获取AccessToken失败:', error);
    throw error;
  }
};

// 调用豆包API的函数
export const chatWithDoubao = async (messages) => {
  try {
    const accessToken = await getAccessToken();
    const response = await axios.post(
      'https://open.volcengine.com/api/v1/doubao/openapi/chat/completions',
      {
        model: 'doubao-0627',
        messages: messages
      },
      {
        headers: {
          'Authorization': `Bearer ${accessToken}`
        }
      }
    );
    return response.data.choices[0].message.content;
  } catch (error) {
    console.error('调用豆包API失败:', error);
    throw error;
  }
};